package ru.gerunn.silent.io;

import java.io.IOException;
import java.io.ObjectInputStream;

import javax.net.ssl.SSLSocket;

import ru.gerunn.silent.exception.SilentReadMessageException;
import ru.gerunn.silent.exception.SilentReaderException;
import ru.gerunn.silent.message.Message;

public class SocketReader implements Reader {

	private ObjectInputStream reader;

	public SocketReader(SSLSocket socket) throws SilentReaderException {
		try {
			this.reader = new ObjectInputStream(socket.getInputStream());
		} catch (IOException e) {
			throw new SilentReaderException("Couldn't create reader", e);
		}
	}

	public Message read() throws SilentReadMessageException {
		try {
			return (Message) this.reader.readObject();
		} catch (ClassNotFoundException e) {
			throw new SilentReadMessageException("ClassNotFoundException on class named Message", e);
		} catch (IOException e) {
			throw new SilentReadMessageException("IOException on reading message", e);
		}
	}

}
